﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Windows.Xps;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Org.BouncyCastle.Asn1.Ocsp;
using be.brandweernijlen.adembescherming.dbDataSetTableAdapters;
using System.Data;

namespace be.brandweernijlen.adembescherming.Controllers
{
    public class PDFController
    {
        public static void generatePDF(TableAdapterManager tableManager)
        {

            using (Document doc = new Document())
            {

                PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("temp.pdf", FileMode.Create));

                doc.Open();


                Rectangle page = doc.PageSize;
                PdfPTable head = new PdfPTable(1);
                head.TotalWidth = page.Width;
                
                Paragraph p = new Paragraph("ADEMBESCHERMING 300 BAR");
                p.Alignment = 1;
                doc.Add(p);
                // table data, see code snippet following this one

                foreach (DataRow row in tableManager.TBL_ABDOCTableAdapter.GetData().Select())
                    doc.Add(_stateTable(tableManager.TBL_ABDOCSTATUSTableAdapter.GetDataByAbDoc(int.Parse(row.ItemArray[0].ToString()))));


            }

        }

        public static void generatePDFByBottle(TableAdapterManager tableManager, System.Windows.Forms.CheckedListBox.CheckedItemCollection bottles)
        {

            using (Document doc = new Document())
            {

                PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("temp.pdf", FileMode.Create));

                doc.Open();


                Rectangle page = doc.PageSize;
                PdfPTable head = new PdfPTable(1);
                head.TotalWidth = page.Width;

                Paragraph p = new Paragraph("ADEMBESCHERMING 300 BAR");
                p.Alignment = 1;
                doc.Add(p);

                foreach (String row in bottles)
                    doc.Add(_stateTable(tableManager.TBL_ABDOCSTATUSTableAdapter.GetDataByBottles(int.Parse(row))));
            }

        }

        // add a table to the PDF document
        private static PdfPTable _stateTable(dbDataSet.TBL_ABDOCSTATUSDataTable dataTable)
        {
            string[] col = { "Nr.", "DATUM", "DRUK", "VERANTWOORDELIJKE" };
            PdfPTable table = new PdfPTable(4);
            /*
            * default table width => 80%
            */
            table.WidthPercentage = 100;
            // then set the column's __relative__ widths
            table.SetWidths(new Single[] { 1, 2, 2, 5 });
            /*
            * by default tables 'collapse' on surrounding elements,
            * so you need to explicitly add spacing
            */
            table.SpacingBefore = 10;

            for (int i = 0; i < col.Length; ++i)
            {
                PdfPCell cell = new PdfPCell(new Phrase(col[i]));
                cell.BackgroundColor = new BaseColor(204, 204, 204);
                table.AddCell(cell);
            }

            // !! database code omitted !!
            // r.Read is the DbDataReader for whatever flavor 
            // of database you're connecting to; we're iterating
            // over the results returned from the database and 
            // adding rows to the table in the PDF 
            for (int j = 0; j < dataTable.Rows.Count; j++)
            {
                DataRow row = dataTable.Rows[j];
                table.AddCell(row[5].ToString());
                table.AddCell(DateTime.Parse(row[1].ToString()).ToShortDateString());
                table.AddCell(row[2].ToString());
                table.AddCell(row[6].ToString());
            }
            return table;
        }

    }


}



